﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
using System.Collections.ObjectModel;
using libConn;
using System.IO;
using CodeReason.Reports;
using System.Windows.Xps.Packaging;

namespace ApartmentSale
{
    /// <summary>
    /// Interaction logic for AffirmanceSelector.xaml
    /// </summary>
    public partial class AffirmanceSelector : Window
    {
        public AffirmanceSelector()
        {
            InitializeComponent();
            selectedAffirmance = new AffirmanceView();
        }

        public AffirmanceSelector(AffirmanceView affirmance) 
            : this()
        {
            selectedAffirmance = affirmance;
        }

        private AffirmanceView selectedAffirmance;

        private bool getData()
        {
            string errMessage = null;
            string str = "SELECT * FROM affirmance WHERE affirmance_status = 0 ORDER BY receipt_code";

            DataTable oleDT;
            oleDT = libConn.AccdbConn.query(str, ref errMessage);

            if (errMessage != null)
            {
                System.Windows.MessageBox.Show(
                    errMessage,
                    "An error occurred while reading data affirmance.",
                    System.Windows.MessageBoxButton.OK,
                    System.Windows.MessageBoxImage.Error);
                return false; ;
            }

            ObservableCollection<AffirmanceView> affirmance = new ObservableCollection<AffirmanceView>();

            foreach (DataRow r in oleDT.Rows)
            {
                affirmance.Add(new AffirmanceView()
                {
                    affirmance_id = Convert.ToInt32(r["affirmance_id"]),
                    affirmance_status = Convert.ToInt32(r["affirmance_status"]),
                    receipt_id = Convert.ToInt32(r["receipt_id"]),
                    receipt_code = r["receipt_code"].ToString().Replace("''", "'"),
                    affirmance_amount = Convert.ToDouble(r["affirmance_amount"]),
                    affirmance_rule_type = r["affirmance_rule_type"].ToString().Replace("''", "'"),
                    affirmance_rule_rebate_amount = Convert.ToDouble(r["affirmance_rule_rebate_amount"]),
                    affirmance_custom_name = r["affirmance_custom_name"].ToString().Replace("''", "'"),
                    affirmance_custom_contact = r["affirmance_custom_contact"].ToString().Replace("''", "'"),
                    affirmance_create_date = Convert.ToDateTime(r["affirmance_create_date"])
                });
            }

            dataGrid.DataContext = affirmance;

            return true;
        }

        private void affirmance_Loaded(object sender, RoutedEventArgs e)
        {
            if (!getData())
                this.Close();
        }

        private void btSelect_Click(object sender, RoutedEventArgs e)
        {
            if (dataGrid.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a receipt information affirmance.");
            }

            AffirmanceView affirmance = dataGrid.SelectedItem as AffirmanceView;
            if (affirmance != null)
            {
                affirmance.CopyTo(selectedAffirmance);
                this.Close();
            }
        }

        private void btClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
